package com.bocai.eelly.activitys;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.bocai.eelly.R;
import com.bocai.eelly.bean.RegistBean;
import com.bocai.eelly.common.BaseActivity;
import com.bocai.eelly.common.Loading;
import com.bocai.eelly.common.ToolbarHelper;
import com.bocai.eelly.event.MainEvent;
import com.bocai.eelly.utils.L;
import com.bocai.eelly.utils.SP;
import com.bocai.eelly.utils.T;
import com.bocai.eelly.utils.Utils;

import butterknife.Bind;
import butterknife.ButterKnife;
import de.greenrobot.event.EventBus;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class RegistPsdActivity extends BaseActivity{
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.et_regist_username)
    EditText etRegistUsername;
    @Bind(R.id.et_regist_pwd)
    EditText etRegistPwd;
    @Bind(R.id.et_regist_rpwd)
    EditText etRegistRpwd;
    @Bind(R.id.btn_regist)
    Button btnRegist;
    private String code;
    private String phone;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_regist_psd);
        ButterKnife.bind(this);
        code = getIntent().getStringExtra("code");
        phone = getIntent().getStringExtra("phone");
        initView();
        initEvent();
    }


    private void initView() {
        ToolbarHelper.setup(this, R.string.str_title_regist, R.mipmap.ic_back, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
    }

    private void initEvent() {
        btnRegist.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(Utils.isEmpty(etRegistUsername.getText().toString())){
                    T.showShort(RegistPsdActivity.this,getString(R.string.str_username_notnull));
                }else if (Utils.isEmpty(etRegistPwd.getText().toString()))
                {
                    T.showShort(RegistPsdActivity.this,getString(R.string.str_pwd_notnull));
                }else if (Utils.isEmpty(etRegistRpwd.getText().toString()))
                {
                    T.showShort(RegistPsdActivity.this,getString(R.string.str_pwdtrue_notnull));
                }else if (!etRegistPwd.getText().toString().equals(etRegistRpwd.getText().toString()))
                {
                    T.showShort(RegistPsdActivity.this,getString(R.string.str_psd_notequals));
                }else {
                    regist();
                }
            }
        });
    }

    private void regist() {
        Loading.show(this,getString(R.string.str_loading));
        String timeline= Utils.getTimeline();
        String sign=Utils.md5(timeline);
        getTestApi().regist(timeline,sign,etRegistUsername.getText().toString(),Utils.base64Encode(etRegistPwd.getText().toString()),
                Utils.base64Encode(etRegistRpwd.getText().toString()),"",phone,code)
                .compose(this.<RegistBean>bindToLifecycle())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<RegistBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        Loading.dismiss();
                        L.d(e.toString());
                        T.showLong(RegistPsdActivity.this,R.string.net_error);
                    }

                    @Override
                    public void onNext(RegistBean baseBean) {
                        Loading.dismiss();
                        T.showLong(RegistPsdActivity.this,baseBean.getMsg());
                        if (Utils.checkResultCode(baseBean.getReturn_code()))
                        {
                            SP.put(RegistPsdActivity.this,"id",baseBean.getData().getId());
                            SP.put(RegistPsdActivity.this,"username",baseBean.getData().getUsername());
                            SP.put(RegistPsdActivity.this,"phone",baseBean.getData().getPhone());
                            EventBus.getDefault().post(new MainEvent("registSuccese"));
                            startActivity(new Intent(RegistPsdActivity.this,PerfectInformationActivity.class));
                            finish();
                        }
                    }
                });
    }
}
